package com.gee.spring.boot.rpf.client.calculator.factor;

import com.gee.spring.boot.rpf.client.calculator.Calculator;
import com.gee.spring.boot.rpf.client.BaseClientResponse;
import com.gee.spring.boot.rpf.common.constant.ClientConstant;
import com.gee.thread.flow.common.result.ExecuteState;

/**
 * desc:
 *
 * @author gee wrote on  2021-02-07 13:50:29
 */
public abstract class AbstractClientCalculator<T> implements Calculator, ClientFactorCalculator<T> {


    @Override
    @SuppressWarnings("unchecked")
    public Object calculate(ExecuteState executeState, BaseClientResponse response, Class factorType, Object... otherParam){
        if (ExecuteState.EXCEPTION == executeState){
            return getClientExceptionFactor(factorType);
        }
        if (! ClientConstant.CLIENT_SUCCESS_CODE.equals(response.getCode())){
            return getResponseCodeNotSuccessFactor(factorType);
        }
        Object result;
        try {
            result = process((T) response, otherParam);
        } catch (Exception e) {
            result = getCalculateExceptionFactor(factorType);
        }
        return result;
    }


}
